package com.dingwen.treasure.base.util;

import cn.hutool.core.convert.Convert;
import lombok.experimental.UtilityClass;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 　ConvertUtil: 类型转换工具
 * 　@author dingwen
 * 　@date 2022/7/29
 */
@UtilityClass
public class ConvertUtil {

    /**
     * 默认分割符
     */
    private static final String SEPARATOR = ",";

    /**
     * 按照指定分割符将字符串转为List
     *
     * @param separator 分隔符
     * @param str       str
     * @param clazz     clazz 集合类型
     * @return {@link List}<{@link T}>
     */
    public  <T> List<T> strToList(String separator, Class<T> clazz, String str) {
        Assert.hasText(separator, "分割符不能为空");
        Assert.hasText(str, "字符串参数不能为空");
        return Convert.toList(clazz, str.split(separator));
    }

    /**
     * 按照指定分割符将字符串转为List
     *
     * @param str   str
     * @param clazz clazz 集合类型
     * @return {@link List}<{@link T}>
     */
    public  <T> List<T> strToList(Class<T> clazz, String str) {
        Assert.hasText(str, "字符串参数不能为空");
        return Convert.toList(clazz, str.split(SEPARATOR));
    }

    /**
     * 按照指定分割符将字符串转为List
     *
     * @param str str
     * @return {@link List}<{@link String}>
     */
    public  List<String> strToList(String str) {
        Assert.hasText(str, "字符串参数不能为空");
        return Convert.toList(String.class, str.split(SEPARATOR));
    }
}
